'''
图片转字符画
'''
class I2Char(CharFrame):
    result = None

    def __init__(self, path, limitSize=-1, fill=False, wrap=False):
        self.genCharImage(path, limitSize, fill, wrap)

        # 从图片文件转为字符画

    def genCharImage(self, path, limitSize=-1, fill=False, wrap=False):
        # 方法读取图片文件，生成的对象我们赋值给 img
        img = Image.open(path)
        self.result = self.convert(img)
        return self.result

    def show(self, stream=2):
        if self.result is None:
            return
        if stream == 1 and os.isatty(sys.stdout.fileno()):
            self.streamOut = sys.stdout.write
            self.streamFlush = sys.stdout.flush
        elif stream == 2 and os.isatty(sys.stderr.fileno()):
            self.streamOut = sys.stderr.write
            self.streamFlush = sys.stderr.flush
        elif hasattr(stream, 'write'):
            self.streamOut = stream.write
            self.streamFlush = stream.flush
        self.streamOut(self.result)
        self.streamFlush()
        self.streamOut('\n')


if __name__ == '__main__':
    '''
        通过命令行调用图片转字符画功能 ,命令行执行 Python v2char.py t3.jpeg,输出字符画

    '''
    # 命令行输入参数处理
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument('file')  # 输入文件
    # 获取参数
    args = parser.parse_args()
    FILE = args.file
    IMG = I2Char(FILE)
    print(IMG.show())

